﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Xml;

/// <summary>
/// Stores the usage of this tool into an XML file.
/// </summary>
public class FSUsageLogOps
{
    private string xMLtimeStamp = "";
    private string xMLcreator = "";
    private string xMLcodeResult = "";
    private string xMLappVersion = "";

    public string XMLTimeStamp
    {
        get
        {
            return xMLtimeStamp;
        }
        set
        {
            xMLtimeStamp = value;
        }
    }

    public string XMLcreator
    {
        get
        {
            return xMLcreator;
        }
        set
        {
            xMLcreator = value;
        }
    }

    public string XMLcodeResult
    {
        get
        {
            return xMLcodeResult;
        }
        set
        {
            xMLcodeResult = value;
        }
    }

    public string XMLappVersion
    {
        get
        {
            return xMLappVersion;
        }
        set
        {
            xMLappVersion = value;
        }
    }  
            
    public FSUsageLogOps()
	{
		//
		// TODO: Add constructor logic here
		//
	}  
   
    public void UpdateUsageLog(ADobjInfo adobj)    
    {
        
        string sourceFile = WbGetParams.UsageLogFile;

        try
        {
            string fileName = Path.Combine(HttpRuntime.AppDomainAppPath, sourceFile);

            XmlDocument xmlUsageLog = new XmlDocument();
            xmlUsageLog.Load(fileName);

            XmlElement XParentElement = xmlUsageLog.CreateElement("LogEntry");
            XmlElement XTimeStamp = xmlUsageLog.CreateElement("TimeStamp");
            XmlElement XAppVersion = xmlUsageLog.CreateElement("AppVersion");
            XmlElement XCreator = xmlUsageLog.CreateElement("Creator");
            XmlElement XCodeResult = xmlUsageLog.CreateElement("CodeResult");
            XmlElement XUserID = xmlUsageLog.CreateElement("UserID");
            XmlElement XfirstName = xmlUsageLog.CreateElement("firstName");
            XmlElement XfamilyName = xmlUsageLog.CreateElement("familyName");
            XmlElement Xfullname = xmlUsageLog.CreateElement("fullname");
            XmlElement XdistinguishedName = xmlUsageLog.CreateElement("distinguishedName");
            XmlElement XcompanyKey = xmlUsageLog.CreateElement("companyKey");
            XmlElement XpersonnelNumber = xmlUsageLog.CreateElement("personnelNumber");
            XmlElement XstreetAddress = xmlUsageLog.CreateElement("streetAddress");
            XmlElement XpostalCode = xmlUsageLog.CreateElement("postalCode");
            XmlElement Xcity = xmlUsageLog.CreateElement("city");
            XmlElement Xskype = xmlUsageLog.CreateElement("skype");
            XmlElement Xmail = xmlUsageLog.CreateElement("mail");
            XmlElement XtelephoneNumber = xmlUsageLog.CreateElement("telephoneNumber");
            XmlElement XmemberOf = xmlUsageLog.CreateElement("memberOf");
            XmlElement XproxyAddresses = xmlUsageLog.CreateElement("proxyAddresses-fax");
            XmlElement XExperationDate = xmlUsageLog.CreateElement("experiationDate");
            XmlElement XExDB = xmlUsageLog.CreateElement("ExDB");
            XmlElement XExMBRetPol = xmlUsageLog.CreateElement("ExMBRetPol");
            XmlElement XExOwaPol = xmlUsageLog.CreateElement("ExOwaPol");
            XmlElement XsID = xmlUsageLog.CreateElement("SID");
            XmlElement XtimeOffset = xmlUsageLog.CreateElement("TimeOffset");      

            XTimeStamp.InnerText = xMLtimeStamp;
            XCreator.InnerText = xMLcreator;
            XAppVersion.InnerText = xMLappVersion;

            if (!String.IsNullOrEmpty(xMLcodeResult))
                XCodeResult.InnerText = xMLcodeResult;
            else
                XCodeResult.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADUserID))
                XUserID.InnerText = adobj.ADUserID;
            else
                XUserID.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADfamilyName))
                XfamilyName.InnerText = adobj.ADfamilyName;
            else
                XfamilyName.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADfullName))
                Xfullname.InnerText = adobj.ADfullName;
            else
                Xfullname.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADfirstName))
                XfirstName.InnerText = adobj.ADfirstName;
            else
                XfirstName.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADdistinguishedName))
                XdistinguishedName.InnerText = adobj.ADdistinguishedName;
            else
                XdistinguishedName.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADcompanyKey))
                XcompanyKey.InnerText = adobj.ADcompanyKey;
            else
                XcompanyKey.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADpersonnelNumber))
                XpersonnelNumber.InnerText = adobj.ADpersonnelNumber;
            else
                XpersonnelNumber.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADstreetAddress))
                XstreetAddress.InnerText = adobj.ADstreetAddress;
            else
                XstreetAddress.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADpostalCode))
                XpostalCode.InnerText = adobj.ADpostalCode;
            else
                XpostalCode.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADCity))
                Xcity.InnerText = adobj.ADCity;
            else
                Xcity.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADSkype))
                Xskype.InnerText = adobj.ADSkype;
            else
                Xskype.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADMail))
                Xmail.InnerText = adobj.ADMail;
            else
                Xmail.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADtelelphoneNumber))
                XtelephoneNumber.InnerText = adobj.ADtelelphoneNumber;                                                               
            else
                XtelephoneNumber.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADproxyAddresses))
                XproxyAddresses.InnerText = adobj.ADproxyAddresses;
            else
                XproxyAddresses.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADmemberOf.ToString()))
            {
                string grps = "";
                foreach (string grp in adobj.ADmemberOf)
                {
                    grps += grp;
                }
                XmemberOf.InnerText = grps;
            }
            else
            {
                XmemberOf.InnerText = "";
            }

            if (!String.IsNullOrEmpty(adobj.ADExperiationDate.ToString()))
            {
                if (adobj.ADExperiationDate > DateTime.Today)
                {
                    XExperationDate.InnerText = adobj.ADExperiationDate.ToString();
                }                    
                else
                {
                    XExperationDate.InnerText = "not specified";               
                }                      
            }                
            else
            {
                XExperationDate.InnerText = "";
            }                

            if (!String.IsNullOrEmpty(adobj.ADExDB))
                XExDB.InnerText = adobj.ADExDB;
            else
                XExDB.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADExMBRetPol))
                XExMBRetPol.InnerText = adobj.ADExMBRetPol;
            else
                XExMBRetPol.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADExOwaPol))
                XExOwaPol.InnerText = adobj.ADExOwaPol;
            else
                XExOwaPol.InnerText = "";

            if (!String.IsNullOrEmpty(adobj.ADSID))
                XsID.InnerText = adobj.ADSID;
            else
                XsID.InnerText = "";          

            if (!String.IsNullOrEmpty(adobj.ADTimeOffset.ToString()))
                XtimeOffset.InnerText = adobj.ADTimeOffset.ToString();
            else
                XtimeOffset.InnerText = "";

            XParentElement.AppendChild(XTimeStamp);
            XParentElement.AppendChild(XCreator);
            XParentElement.AppendChild(XAppVersion);            
            XParentElement.AppendChild(XUserID);
            XParentElement.AppendChild(XfirstName);
            XParentElement.AppendChild(XfamilyName);
            XParentElement.AppendChild(XdistinguishedName);
            XParentElement.AppendChild(XcompanyKey);
            XParentElement.AppendChild(XpersonnelNumber);
            XParentElement.AppendChild(XstreetAddress);
            XParentElement.AppendChild(XpostalCode);
            XParentElement.AppendChild(Xcity);
            XParentElement.AppendChild(Xskype);
            XParentElement.AppendChild(Xmail);
            XParentElement.AppendChild(XtelephoneNumber);
            XParentElement.AppendChild(XmemberOf);
            XParentElement.AppendChild(XproxyAddresses);
            XParentElement.AppendChild(XExperationDate);
            XParentElement.AppendChild(XExDB);
            XParentElement.AppendChild(XExMBRetPol);
            XParentElement.AppendChild(XExOwaPol);
            XParentElement.AppendChild(XsID);            
            XParentElement.AppendChild(XtimeOffset);
            XParentElement.AppendChild(XCodeResult);     

            xmlUsageLog.DocumentElement.AppendChild(XParentElement);
            xmlUsageLog.Save(fileName);
        
        }

        catch (Exception ex)
        {            
            string retMsg = ex.Message;
        }

    }

}